package com.xuanwei.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuanwei.core.annos.Positive;
import com.xuanwei.core.cache.HomePageCache;
import com.xuanwei.core.entity.vo.HomePageVo;
import com.xuanwei.core.entity.vo.NavigateVo;
import com.xuanwei.core.enums.NavigatePosition;
import com.xuanwei.core.response.ResultVO;
import com.xuanwei.core.service.IXwHomepageNavigateService;
import com.xuanwei.dao.entity.XwHomepageNavigate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/8/15 14:55
 **/
@RestController
@RequestMapping("wx")
@Validated
public class HomepageController {

    private HomePageCache homePageCache;

    private IXwHomepageNavigateService homepageNavigateService;

    public HomepageController(HomePageCache homePageCache,
                              IXwHomepageNavigateService homepageNavigateService) {
        this.homePageCache = homePageCache;
        this.homepageNavigateService = homepageNavigateService;
    }

    /**
     * 小程序首页
     *
     * @return 数据
     */
    @RequestMapping(value = "homepage/index", method = RequestMethod.GET)
    public ResultVO index() {
        HomePageVo homePageVo = homePageCache.getHomePage();
        return ResultVO.successResult(homePageVo);
    }

    @RequestMapping(value = "activity/{id}", method = RequestMethod.GET)
    public ResultVO activityDetail(@PathVariable("id") @Positive(message = "ID参数错误") Integer id) {
        NavigateVo activityDetail = homePageCache.getActivityDetail(id);
        return ResultVO.successResult(activityDetail);
    }

    @RequestMapping(value = "recommendProduct", method = RequestMethod.GET)
    public ResultVO recommendProduct() {
        XwHomepageNavigate one = homepageNavigateService.getOne(new QueryWrapper<XwHomepageNavigate>()
                .select("id")
                .eq("position", NavigatePosition.RECOMMEND.getCode()));
        if (one == null) {
            return ResultVO.successResult();
        }
        NavigateVo activityDetail = homePageCache.getActivityDetail(one.getId());
        return ResultVO.successResult(activityDetail.getProductDtos());
    }


}
